#!/bin/bash

THEMES="default new-year halloween x-mas easter iron-girl"

set -e


CB='\E[1m'
CB1='\E[1;30m'
CB2='\E[1;34m'
CERROR='\E[1;31m'
CE='\E[0;00m'

if [ -f Makefile ]; then
    echo -e "${CB}Running 'make distclean':${CE}"
    make distclean
    echo -e "${CB}Done running 'make distclean'.${CE}"
    echo ""
fi


echo -en "${CB}Generating files:${CE}"

######################################
# src/Makefile.am:

fn=src/Makefile.am
echo -en "${CB1} ${fn}${CE}"


echo '
if WITH_COMPRESSED_DATA
   WCD_CFLAGS=-DWITH_COMPRESSED_DATA
endif

if WITH_OPENGL

if WITH_LIB3DS_V1
LIB3DS_V1=-DLIB3DS_V1
endif

OGL_CFLAGS = \
	-DWITH_OPENGL         \
	-DGL_GLEXT_PROTOTYPES \
	$(gl_CFLAGS)          \
	$(glew_CFLAGS)        \
	$(glu_CFLAGS)         \
	$(LIB3DS_CFLAGS) $(LIB3DS_V1)

OGL_LIBS = \
	$(gl_LIBS)     \
	$(glew_LIBS)   \
	$(glu_LIBS)    \
	$(LIB3DS_LIBS)
endif

if WITH_NONOPENGL
NONOGL_CFLAGS = -DWITH_NONOPENGL
NONOGL_LIBS =
endif

if WITH_CHECKSUMMING
CHECKSUMMING_CFLAGS = -DWITH_CHECKSUMMING
else
CHECKSUMMING_CFLAGS =
endif

if WITH_MIKMODFREE
MIKMODFREE_CFLAGS = -DWITH_MIKMODFREE
else
MIKMODFREE_CFLAGS =
endif

if WITH_WIN32
WIN32_LDADD = windows32.o
else
WIN32_LDADD =
endif

AM_CFLAGS = \
	$(DEBUGGING_CFLAGS)      \
	-DDIAMOND_GIRL_DATADIR=\"$(datadir)\" \
	$(WCD_CFLAGS)            \
	$(CHECKSUMMING_CFLAGS)   \
	$(OGL_CFLAGS)            \
	$(NONOGL_CFLAGS)         \
	$(CWARNFLAGS)            \
	$(libmikmod_CFLAGS)      \
	$(LIBMIKMOD_CFLAGS)      \
	$(MIKMODFREE_CFLAGS)     \
	$(libpng_CFLAGS)         \
	$(lua_CFLAGS)            \
        $(json_CFLAGS)           \
	$(openal_CFLAGS)         \
	$(sdl_CFLAGS)            \
	$(sdl_image_CFLAGS)      \
	$(sndfile_CFLAGS)        \
	$(vorbisfile_CFLAGS)

bin_PROGRAMS = diamond_girl

' > ${fn}

echo -n 'diamond_girl_SOURCES = ' >> ${fn}
for i in $(dirname ${fn})/*.[ch] ; do 
    echo -n "$(basename ${i}) " >> ${fn}
done
echo 'mtwist-1.1/mtwist.c mtwist-1.1/mtwist.h
' >> ${fn}

echo '
diamond_girl_LDADD =       \
	$(OGL_LIBS)        \
	$(NONOGL_LIBS)     \
	$(libmikmod_LIBS)  \
	$(LIBMIKMOD_LIBS) $(LIBMIKMOD_LDADD) \
	$(libpng_LIBS)     \
	$(lua_LIBS)        \
	$(json_LIBS)       \
	$(openal_LIBS)     \
	$(sdl_LIBS)        \
	$(sdl_image_LIBS)  \
	$(sndfile_LIBS)    \
	$(vorbisfile_LIBS) \
	$(WIN32_LDADD)
' >> ${fn}

echo '
EXTRA_DIST = \
	mtwist-1.1/CHANGELOG   \
	mtwist-1.1/README      \
	mtwist-1.1/mtwist.3    \
	mtwist-1.1/randistrs.3 \
	mtwist-1.1/randistrs.c \
	mtwist-1.1/randistrs.h

if WITH_WIN32
diamond_girl.ico: ../themes/default/gfx/desktop-icon.png
	$(AM_V_GEN) $(ICOTOOL) --create --output=$@ $<

windows32.o: windows32.rc diamond_girl.ico
	$(AM_V_GEN) $(WINDRES) --input=$< --output=$@ --output-format=coff
endif

CLEANFILES = diamond_girl.ico

' >> ${fn}




for theme in ${THEMES} ; do

    if [ -d themes/${theme}/sfx ]; then
        ######################################
        # sfx/Makefile.am:

        fn=themes/${theme}/sfx/Makefile.am
        echo -en "${CB1} ${fn}${CE}"
        echo "" > ${fn}

        echo "sfxdir = \$(pkgdatadir)/themes/${theme}/sfx" >> ${fn}
        echo "" >> ${fn}
        
        sfxfiles=""
        for i in themes/${theme}/sfx/*.{sfx,xm} ; do
            if [ -f ${i} ]; then
                sfxfiles+="$(basename ${i}) "
            fi
        done

        wavfiles=""
        oggfiles=""
        for i in themes/${theme}/sfx/*.wav ; do
            if [ -f ${i} ]; then
                wavfiles+="$(basename ${i}) "
                oggfiles+="$(basename ${i} .wav).ogg "
            fi
        done
        
        echo 'if WITH_COMPRESSED_DATA' >> ${fn}
        echo "" >> ${fn}

        oggenc="oggenc --quality=3 --quiet"
        for i in themes/${theme}/sfx/*.wav ; do
            if [ -f ${i} ]; then
                echo "$(basename ${i} .wav).ogg: \$(srcdir)/$(basename ${i})" >> ${fn}
                echo "	\$(AM_V_GEN) ${oggenc} \$< -o \$@"                >> ${fn}
                echo ""                                                       >> ${fn}
            fi
        done
        for i in themes/${theme}/sfx/*.xm ; do
            if [ -f ${i} ]; then
                echo "$(basename ${i} .xm).ogg: $(basename ${i} .xm).wav" >> ${fn}
                echo "	\$(AM_V_GEN) ${oggenc} \$< -o \$@"            >> ${fn}
                echo ""                                                   >> ${fn}
            fi
        done

        echo "sfx_DATA = ${sfxfiles} ${oggfiles}" >> ${fn}
        echo "EXTRA_DIST = ${sfxfiles} ${oggfiles}" >> ${fn}

        echo "" >> ${fn}
        echo 'else' >> ${fn}
        echo "" >> ${fn}

        echo "sfx_DATA = ${sfxfiles} ${wavfiles}" >> ${fn}
        echo "EXTRA_DIST = ${sfxfiles} ${wavfiles}" >> ${fn}

        echo 'endif' >> ${fn}
        echo "" >> ${fn}
        
        
        echo -n "CLEANFILES = " >> ${fn}
        for i in themes/${theme}/sfx/*.wav ; do
            if [ -f ${i} ]; then
                echo -n "$(basename ${i} .wav).ogg " >> ${fn}
            fi
        done
        echo "" >> ${fn}
        
    fi
    
    
    
    ######################################
    # gfx/Makefile.am:
    
    fn=themes/${theme}/gfx/Makefile.am
    echo -en "${CB1} ${fn}${CE}"
    
    echo "gfxdir = \$(pkgdatadir)/themes/${theme}/gfx" > ${fn}
    
    cleanfiles=""
    extradist=""
    gfxdata=""
    for xcf in themes/${theme}/gfx/*.xcf ; do
        if [ -f ${xcf} ]; then
            extradist+="$(basename ${xcf}) "
            xcfhasfiles=0
            for file in $(xcfinfo ${xcf} | awk '/^. [0-9]/{print $5;}') ; do
                if [ ${file:0:2} != "w_" ]; then
                    gfxdata+="${file}.png "
                    cleanfiles+="${file}.png "
                    echo -n "${file}.png " >> ${fn}
                    xcfhasfiles=1
                fi
            done
            if [ ${xcfhasfiles} -ne 0 ]; then
                echo ": $(basename ${xcf})" >> ${fn}
                echo '	$(AM_V_GEN) xcf2png --autocrop $< $$(basename $@ .png) > $@' >> ${fn}
                echo "if WITH_COMPRESSED_DATA" >> ${fn}
                echo '	$(AM_V_GEN) optipng -nc -quiet $@' >> ${fn}
                echo "endif" >> ${fn}
                echo "" >> ${fn}
            fi
        fi
    done
    for i in themes/${theme}/gfx/*.3ds ; do
        if [ -f ${i} ]; then
            gfxdata+="$(basename ${i}) "
            extradist+="$(basename ${i}) "
        fi
    done
    for i in themes/${theme}/gfx/*.png.cnf ; do
        if [ -f ${i} ]; then
            gfxdata+="$(basename ${i}) "
            extradist+="$(basename ${i}) "
        fi
    done
    for i in themes/${theme}/gfx/*.txt.bz2 ; do
        if [ -f ${i} ]; then
            gfxdata+="$(basename ${i}) "
            extradist+="$(basename ${i}) "
        fi
    done


    # Special cases for gfx

    if [ -f themes/${theme}/gfx/desktop-icon.xcf -o -f themes/${theme}/gfx/desktop-icon.png ]; then

        cleanfiles+=" diamond_girl.png"

        echo '
pixmapsdir = $(datadir)/pixmaps
pixmaps_DATA = diamond_girl.png
' >> ${fn}

        echo '
diamond_girl.png: desktop-icon.png
	$(AM_V_GEN) cp $< $@
' >> ${fn}

    fi


    echo "gfx_DATA = ${gfxdata}" >> ${fn}
    
    echo "CLEANFILES = ${cleanfiles}" >> ${fn}

    echo "EXTRA_DIST = ${extradist}" >> ${fn}





    if [ -d themes/${theme}/txt ]; then
        ######################################
        # txt/Makefile.am:

        fn=themes/${theme}/txt/Makefile.am
        echo -en "${CB1} ${fn}${CE}"

        echo "txtdir = \$(pkgdatadir)/themes/${theme}/txt" > ${fn}

        cleanfiles=""
        extradist=""
        txtdata=""
        for txt in themes/${theme}/txt/*.txt ; do
            if [ -f ${txt} ]; then
                txtdata+=" $(basename ${txt})"
            fi
        done


        echo "txt_DATA = ${txtdata}" >> ${fn}

        if [ -n "${cleanfiiles}" ]; then
            echo "CLEANFILES = ${cleanfiles}" >> ${fn}
        fi

        if [ -n "${extradist}" ]; then
            echo "EXTRA_DIST = ${extradist}" >> ${fn}
        fi

    fi


done

######################################
# data/Makefile.am:

fn=data/Makefile.am
echo -en "${CB1} ${fn}${CE}"

echo 'extradatadir = $(pkgdatadir)/data
' > ${fn}

echo -n 'extradata_DATA = ' >> ${fn}
for i in data/*.bz2 ; do
    echo -n "$(basename ${i}) " >> ${fn}
done
echo "
" >> ${fn}

echo 'EXTRA_DIST = $(extradata_DATA)
' >> ${fn}



######################################
# ui/Makefile.am:

fn=ui/Makefile.am
echo -en "${CB1} ${fn}${CE}"

echo 'extradatadir = $(pkgdatadir)/ui
' > ${fn}

echo -n 'extradata_DATA = ' >> ${fn}
for i in ui/*.bz2 ; do
    echo -n "$(basename ${i}) " >> ${fn}
done
echo "
" >> ${fn}

echo 'EXTRA_DIST = $(extradata_DATA)
' >> ${fn}




######################################
# maps/Makefile.am:

fn=maps/Makefile.am
echo -en "${CB1} ${fn}${CE}"

extradist=""

echo "" > ${fn}
for dir in maps/* ; do
    if [ -d ${dir} ]; then
        dn=$(basename ${dir})
        extradist+="\$(${dn}_maps_DATA) "
        echo "${dn}_mapsdir = \$(pkgdatadir)/maps/${dn}" >> ${fn}
        echo -n "${dn}_maps_DATA = " >> ${fn}
        for map in ${dir}/* ; do
            echo -n "$dn/$(basename ${map}) " >> ${fn}
        done
        echo "
" >> ${fn}
    fi
done

echo "EXTRA_DIST = ${extradist}
" >> ${fn}





######################################
# themes/Makefile.am:

fn=themes/Makefile.am
echo -en "${CB1} ${fn}${CE}"
echo 'extradatadir = $(pkgdatadir)/themes
' > ${fn}
echo -n 'extradata_DATA =' >> ${fn}
for file in themes/*.bz2 ; do
    echo -n " $(basename ${file})" >> ${fn}
done
echo "
" >> ${fn}
echo 'EXTRA_DIST = $(extradata_DATA)
' >> ${fn}
echo "SUBDIRS = ${THEMES}" >> ${fn}


for theme in ${THEMES} ; do
    fn=themes/${theme}/Makefile.am
    echo -en "${CB1} ${fn}${CE}"
    echo -n "SUBDIRS =" > ${fn}
    for sd in gfx sfx ; do
        if [ -d themes/${theme}/${sd} ]; then
            echo -n " ${sd}" >> ${fn}
        fi
    done
    echo "" >> ${fn}
done

echo ""
echo ""


######################################
# configure.ac

echo -en "${CB}Updating AC_CONFIG_FILE in configure.ac...${CE}"

AC_CONFIG_FILES="Makefile src/Makefile maps/Makefile data/Makefile themes/Makefile ui/Makefile"
for theme in ${THEMES} ; do
    AC_CONFIG_FILES+=" themes/${theme}/Makefile"
    for sd in gfx sfx txt ; do
        if [ -d themes/${theme}/${sd} ]; then
            AC_CONFIG_FILES+=" themes/${theme}/${sd}/Makefile"
        fi
    done
done
tmp=$(echo ${AC_CONFIG_FILES} | sed 's/\//\\\//g')
sed -i "s/^\(AC_CONFIG_FILES(\[\).*\(\])\)/\1${tmp}\2/g" configure.ac

echo -e " done."
echo ""



######################################
# autoreconf

echo -en "${CB}Running 'autoreconf -i'...${CE}"
autoreconf -i

echo -e " done."
echo ""
